﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.LanguageWire.DAL.LanguageWireDAL;

namespace iLangl.LanguageWire.Managers
{
	public class ProgressManager
	{
		#region Singelton Pattern
		private ProgressManager()
		{
		}

		private static ProgressManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the ProgressManager.
		/// </summary>
		public static ProgressManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new ProgressManager();
						}
					}
				}
				return instance;
			}
		}
		#endregion


		public void RecalculateProgress(List<JobInfo> jis)
		{
			int progress = 0;
			foreach (JobInfo ji in jis)
			{
				progress = 0;

				TranslationStatuses.eTranslationStatus status = MappingManager.Instance.GetTranslationStatus(ji.Status);

					if (status == TranslationStatuses.eTranslationStatus.Finished)
					{
						progress = 100;
						continue;
					}

				if (ji.IsParent()&&(ji.Translation != null))
				{
						foreach (JobInfo childji in ji.JobInfos)
						{
							status = MappingManager.Instance.GetTranslationStatus(childji.Status);
							if (status == TranslationStatuses.eTranslationStatus.Finished)
							{
								progress += 100;
							}
						}

						progress = progress / 100;

						if (progress == 100)
							progress = 99; //Mother assignment is not finished
					
				}
				else if ((ji.Translation != null)&&(status == TranslationStatuses.eTranslationStatus.InProgress))
				{
					progress =  DALFacadeLW.Instance.ProjectDAL.GetProgress(ji);
				}


				if (ji.Translation != null)
					ji.Translation.Progress = progress;
			}
		}
	}
}
